//
//  WYADView.m
//  广告图
//
//  Created by 王琰 on 15/3/7.
//  Copyright (c) 2015年 王琰. All rights reserved.
//

#import "WYADView.h"
#import "UIImageView+WebCache.h"
//获取当前屏幕的宽度
#define WWidth  self.frame.size.width

@interface WYADView ()<UIScrollViewDelegate>
{
    //点击回调block
    PicOnClick _picBlock;
    ////启动时间计数器
    NSTimer *_timer;
}
@end
@implementation WYADView

//获取实例的方法
- (instancetype)initWithFrame:(CGRect)frame AndPics:(NSArray *)pics AndTitles:(NSArray *)titles PicMode:(UIViewContentMode)mode
{

    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        if (pics .count > 0) {
            self.imgs = [NSArray array];
            self.titles = [NSArray array];
            _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
            //设置代理
            _scrollView.delegate = self;
            _scrollView.pagingEnabled = YES;
            //    _scrollView.bounces = NO;
            [self addSubview:_scrollView];
            
            self.imgs = pics;
            self.titles = titles;
            
            //    图片首添加一个虚拟的最后一张 循环显示
            UIImageView *first = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
            first.contentMode = mode;
            first.tag = self.imgs.count + 10;
            NSString *fm = self.imgs[self.imgs.count-1];
            [self addTap:first];
            [first sd_setImageWithURL:[NSURL URLWithString:fm] placeholderImage:[UIImage imageNamed:@"moren"]];
            [self.scrollView addSubview:first];
            NSInteger index = 1;
            for (NSString *imgPath in pics) {
                UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width*index, 0, self.frame.size.width, self.frame.size.height)];
                imv.tag = index;
                [self.scrollView addSubview:imv];
                [imv sd_setImageWithURL:[NSURL URLWithString:imgPath] placeholderImage:[UIImage imageNamed:@"moren"]];
                imv.contentMode = mode;
                [self addTap:imv];
                index++;
            }
            //最后放一个第一个图片 虚拟
            UIImageView *last = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width*(self.imgs.count+1), 0, self.frame.size.width, self.frame.size.height)];
            NSString *lastI = self.imgs[0];
            [last sd_setImageWithURL:[NSURL URLWithString:lastI] placeholderImage:[UIImage imageNamed:@"moren"]];
            last.contentMode = mode;
            [self.scrollView addSubview:last];
            last.tag = 10+1;
            [self addTap:last];
            //设置偏移大小
            //设置起始偏移
            [self.scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
            self.scrollView.contentSize = CGSizeMake(WWidth*(self.imgs.count+2), 0);
            self.scrollView.bounces = NO;
            [self.scrollView setShowsHorizontalScrollIndicator:NO];
            //初始化pagecontroll
            self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width - 100, self.frame.size.height-45, 100, 20)];
            self.pageControl.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, self.frame.size.height-20);
            //默认点的颜色
            self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
            //选中界面时对应点的颜色
            self.pageControl.currentPageIndicatorTintColor = QBB_RED_COLOR;
            self.pageControl.alpha = 0.9;
            //背景颜色
            self.pageControl.backgroundColor = [UIColor clearColor];
            self.pageControl.numberOfPages = pics.count;
            [self addSubview:self.pageControl];
            
            //标题
            if (titles.count != 0 && titles != nil) {
                //初始化titleLable
                self.titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height-25, WWidth, 25)];
                self.titleLab.backgroundColor = [UIColor blackColor];
                //        _titleLab.alpha = 0.8;
                self.titleLab.textColor = [UIColor whiteColor];
                self.titleLab.textAlignment = NSTextAlignmentCenter;
                self.titleLab.text = self.titles[0];
                self.titleLab.font = [UIFont systemFontOfSize:15];
                [self addSubview:self.titleLab];
            }
            
            
            //启动广告图效果
            [self startAd];
        }
    }else{
    
        //添加默认图片
    }
        return self;
}

//添加点击手势
- (void)addTap:(UIImageView *)iv
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mpicsOnClick:)];
    [iv addGestureRecognizer:tap];
    iv.userInteractionEnabled = YES;
}
//图片被点击后的回调事件
- (void)picOnClick:(PicOnClick)picBlock
{
    //记录回调Block
    _picBlock = picBlock;
}
//图片的点击事件
- (void)mpicsOnClick:(UITapGestureRecognizer *)tap
{
    UIImageView *sender = (UIImageView *)tap.view;
    _picBlock(sender.tag%10);
}
#pragma mark - scrollViewDelegate
//协议中方法的实现
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    //界面宽度
    CGFloat swidth = self.frame.size.width;
    //获取当前显示界面
    NSInteger currentPage =  (scrollView.contentOffset.x-swidth)/swidth;
    if (currentPage == -1) {
        [_scrollView setContentOffset:CGPointMake(swidth*(_imgs.count), 0)];
        //显示title
        NSString *title = _titles[_titles.count-1];
        _titleLab.text = title;
        _pageControl.currentPage = _imgs.count;
    }
    else if (currentPage == _imgs.count)
    {
        [_scrollView setContentOffset:CGPointMake(swidth, 0)];
        _pageControl.currentPage = 0;
        //显示title
        NSString *title = _titles[0];
        _titleLab.text = title;
    }
    else
    {
        //关联点与界面
        _pageControl.currentPage = currentPage;
        //显示title
        NSString *title = _titles[currentPage];
        _titleLab.text = title;
        
    }
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self endOnClick];
}
- (void)startAd
{
    //启动时间计数器
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(goroll)  userInfo:nil repeats:YES];
    
}
- (void)endOnClick
{
    //结束时间计数器
    [_timer invalidate];
}
//timmer执行的方法体
- (void)goroll
{
    //界面宽度
    CGFloat swidth = self.frame.size.width;
    CGPoint oldPoint = _scrollView.contentOffset;
    //获取当前显示界面
    NSInteger currentPage =  _scrollView.contentOffset.x/swidth;
    
    //关联点与界面
    _pageControl.currentPage = currentPage;
    
    //自动往后走一个
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [_scrollView setContentOffset:CGPointMake(oldPoint.x+swidth, 0)];
    [UIView commitAnimations];
    
    //如果走到最后一个
    if(_scrollView.contentOffset.x/swidth == _imgs.count+1)
    {
        [_scrollView setContentOffset:CGPointMake(swidth, 0)];
        _pageControl.currentPage = 0;
        currentPage = 0;
    }
    //显示title
    //显示title
    NSString *title = _titles[currentPage];
    _titleLab.text = title;
    
    
}
//走动效果标志
- (void)setStopScrollFlag:(BOOL)stopScrollFlag
{
    _stopScrollFlag = stopScrollFlag;
    if (_stopScrollFlag) {
        [_timer invalidate];
        [_scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    }else{
        [self startAd];
    }
}
- (void)dealloc
{
    //结束时间计数器
    [_timer invalidate];
    
}
@end
